package com.sqstudio.common
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;

	public final class DataProxy
	{
		public function DataProxy()
		{
			creatHolidayWorkArr();
		}
		/**
		 *判断d1和d2是否为同一天 
		 * @param $d1
		 * @param $d2
		 * @return 
		 * 
		 */		
		public　static function equalsDate($d1:Date,$d2:Date):Boolean{
			var yBoo:Boolean = $d1.getFullYear() == $d2.getFullYear();
			var mBoo:Boolean = $d1.getMonth() == $d2.getMonth();
			var dBoo:Boolean = $d1.getDate() == $d2.getDate();
			return yBoo && mBoo && dBoo;
		}
		/**
		 *将d1赋值为d2
		 * @param $d1
		 * @param $d2
		 * @return 
		 * 
		 */		
		public　static function getDateCopy($d:Date):Date{
			var $d1:Date = new Date($d.fullYear,$d.month,$d.date);
			return $d1;
		}
		/**
		 *设置元件Index 
		 * @param sp
		 * 
		 */		
		public　static function setIndex(sp:DisplayObject):void{
			var tmpSP:DisplayObject = sp;
			while(tmpSP != null && tmpSP.parent != null){
				tmpSP.parent.setChildIndex(tmpSP,tmpSP.parent.numChildren-1);
				tmpSP = tmpSP.parent as DisplayObject;
			}
		}
		
		
		private static var sFtv:Array = [101,214,308,312,401,501,504,601,701,801,910,1001,1225];
		/**
		 *检查下一个节日 
		 * @param $d
		 * @return 
		 * 
		 */		
		public static function checkNextHoliday($d:Date):Array{
			var tmpMd:int = ($d.month+1) * 100 + $d.date;
			var tarMd:int = sFtv[0];
			var isCrtHoliday:Boolean = false;
			for(var i:int=0;i<sFtv.length;i++){
				if(tmpMd <= sFtv[i]){
					if(tmpMd < sFtv[i]){
						tarMd = sFtv[i];
					}else{
						isCrtHoliday = true;
					}
					break;
				}
			}
			var tarDate:Date = new Date($d.getFullYear(),(int(tarMd/100)-1),tarMd%100);
			if(tarMd == sFtv[0]) tarDate.setFullYear($d.getFullYear()+1);
			var dNum:int = getDaysBetweenDates($d,tarDate);
			var lc:LunarCalendar ;
			if(isCrtHoliday == true){
				dNum=0;
				lc = new LunarCalendar($d);
			}else{
				lc = new LunarCalendar(tarDate)
			}
			return [lc.getsFtv(),dNum];
		}
		
		/**
		 *获得d2和d1直接间隔的天数 （d2-d1） 
		 * @param d1
		 * @param d2
		 * @return 
		 * 
		 */		
		public static function getDaysBetweenDates(d1:Date,d2:Date):int{
			var diffmillSec:Number=d2.getTime()-d1.getTime();
			var diffDays:Number=Math.floor(diffmillSec/(1000*60*60*24));
			return diffDays;
		}
		
		
		
		
		
		//------------ 放假处理
		
		public static function checkIsHoliday($d:Date):Boolean{
			if(_holidayDataArr.length==0) creatHolidayWorkArr();
			
			var isHoliday:Boolean = false;
			var nowDate:Date = new Date();
			if($d.getFullYear() == nowDate.getFullYear()){
				var sitem:int = int($d.getMonth()+1)*100 + int($d.getDate());
				if(_holidayDataArr.indexOf(sitem) != -1){
					isHoliday = true;
				}
			}
			return isHoliday;
		}
		public static function checkIsWork($d:Date):Boolean{
			if(_workDataArr.length==0) creatHolidayWorkArr();
			var isWork:Boolean = false;
			var nowDate:Date = new Date();
			if($d.getFullYear() == nowDate.getFullYear()){
				var sitem:int = int($d.getMonth()+1)*100 + int($d.getDate());
				if(_workDataArr.indexOf(sitem) != -1){
					isWork = true;
				}
			}
			return isWork;
		}
		
		
		private static var _holidayDataArr:Array = new Array();
		private static var _workDataArr:Array = new Array();
		private static function creatHolidayWorkArr():void{
			var xml:XML = DataProxy.holidayXML;
			for(var i:int=0;i<xml.holiday.length();i++){
				var jstart:int = int(String(xml.holiday[i].@startDate));
				var jend:int = int(String(xml.holiday[i].@endDate));
				for(var j:int = jstart;j<=jend;j++){
					_holidayDataArr.push(j);
				}
				for(var k:int=0;k<xml.holiday[i].workDate.length();k++){
					_workDataArr.push(int(String(xml.holiday[i].workDate[k])));
				}
			}
		}
		
		
		
		
		
		//官方放假安排
		public static const holidayXML:XML = <holidays>
							<holiday id="1" name="元旦" startDate="0101" crtDate="0101" endDate="0103">
								<info>元旦放假安排</info>
							</holiday>
							<holiday id="2" name="春节" startDate="0202" crtDate="0203" endDate="0208">
								<info>春节放假安排</info>
								<workDate>0130</workDate>
								<workDate>0212</workDate>
							</holiday>
							
							<holiday id="3" name="清明节" startDate="0403" crtDate="0405" endDate="0405">
								<info>清明节放假安排</info>
								<workDate>0402</workDate>
							</holiday>
							
							<holiday id="4" name="劳动节" startDate="0430" crtDate="0501" endDate="0502">
								<info>劳动节放假安排</info>
							</holiday>
							
							<holiday id="5" name="端午节" startDate="0604" crtDate="0606" endDate="0606">
								<info>端午节放假安排</info>
							</holiday>
							
							<holiday id="6" name="中秋节" startDate="0910" crtDate="0912" endDate="0912">
								<info>中秋节放假安排</info>
							</holiday>
							
							<holiday id="7" name="国庆节" startDate="1001" crtDate="1001" endDate="1007">
								<info>国庆节放假安排</info>
								<workDate>1008</workDate>
								<workDate>1009</workDate>
							</holiday>
						</holidays>;
		
	}
}